package org.taurusxi.apiConsumer.mvc;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.taurusxi.cloudbean.ApiModel;

/**
 * Created by chengxi on 2017/8/14.
 */

@RestController
class ConsumerJavaController {

    @Autowired
    RestTemplate restTemplate = null;


    @HystrixCommand(fallbackMethod = "indexCallback")
    @RequestMapping(path = "/indexObj",method = RequestMethod.GET)
    public ApiModel consumerIndexObj()throws Exception{
        ApiModel model = restTemplate.getForEntity("http://API-PROVIDER/index/hello",ApiModel.class).getBody();
        return model;
    }


    @HystrixCommand(fallbackMethod = "indexStrCallback")
    @RequestMapping(path = "/index",method = RequestMethod.GET)
    public String consumerIndex()throws Exception{
        String model = restTemplate.getForEntity("http://API-PROVIDER/index/hello",String.class).getBody();
        return model;
    }



    @NotNull
    private ApiModel indexCallback(){
        return  new ApiModel(400,"服务异常");
    }

    @NotNull
    private String  indexStrCallback(){
        return  ApiModel.ERROR_MODEL.toString();
    }
}
